//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    using System.Collections.ObjectModel;
    using Microsoft.Practices.Unity;
    using System.Linq;
    using Microsoft.Research.DataLayer;

    /// <summary>
    /// This contains the handling logic for the data products tab of the workflow app.
    /// </summary>
    public partial class DataProductPresentationModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DataProductPresentationModel"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="view">The view.</param>
        public DataProductPresentationModel(IUnityContainer container, DataProductsView view)
        {
            this.container = container;
            View = view;
            this.DataProductsViewCollection = new ObservableCollection<DataProductViewModel>();
            this.dataProductService = container.Resolve<IDataProductService>();
            View.Model = this;
        }

        /// <summary>
        /// Updates the data products. On receiving a data product, adds that to the data products list of that particular data product.
        /// </summary>
        /// <param name="dataProductToUpdate">The data product to update.</param>
        public void UpdateDataProducts(DataProductItem dataProductToUpdate)
        {
            if (dataProductToUpdate != null && dataProductToUpdate.OriginalDataProduct != null)
            {
                string srcActivityName = dataProductToUpdate.OriginalDataProduct.Name;
                string srcActivityLabel = srcActivityName;

                if (dataProductToUpdate.OriginalDataProduct.CreatorActivity != null
                    && dataProductToUpdate.OriginalDataProduct.CreatorActivity.Activity != null)
                {
                    srcActivityName = dataProductToUpdate.OriginalDataProduct.CreatorActivity.Name;
                    srcActivityLabel = string.IsNullOrEmpty(dataProductToUpdate.OriginalDataProduct.CreatorActivity.Activity.Label) ?
                        dataProductToUpdate.OriginalDataProduct.CreatorActivity.Activity.Name :
                        dataProductToUpdate.OriginalDataProduct.CreatorActivity.Activity.Label;
                }

                DataProductViewModel dataProductViewModel = DataProductPresentationModel.LookupOrCreate(
                    srcActivityLabel,
                    srcActivityName,
                    this.DataProductsViewCollection);

                if (dataProductViewModel != null)
                {
                    DataProductItem dpItem = dataProductViewModel.DataProducts.FirstOrDefault(
                        item => (item.OriginalDataProduct as IObject).ID.Equals((dataProductToUpdate.OriginalDataProduct as IObject).ID));
                    if (dpItem == null)
                    {
                        dataProductViewModel.DataProducts.Add(dataProductToUpdate);
                    }
                }
            }
        }
    }
}
